home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HyperLib 1997 Winter - Disc 1
/
HYPERLIB-1997-Winter-CD1.ISO.7z
/
HYPERLIB-1997-Winter-CD1.ISO
/
オンラインウェア
/
UTIL
/
Folder・Icon・Opener 1.0.1.sit
/
Folder•Icon•Opener 1.0.1
/
source code
/
sources
/
FIOpen.file.c
< prev
next >
Wrap
Text File
|
1996-05-05
|
11KB
|
382 lines
/*
*--------------------------------------------------------------
* FIOpen.file.c
*--------------------------------------------------------------
* Copyright ゥ Fumio Rokkaku, 1996
*--------------------------------------------------------------
*/
#pragma once
/* System Headers */
#include <Memory.h>
#include <Files.h>
#include <Folders.h>
#include <Finder.h>
#include <Aliases.h>
#include <Resources.h>
#include <OSUtils.h>
#include <Processes.h>
#include <icons.h>
#include <Errors.h>
/* Project Headers */
#include "FIOpen.h"
/*
*--------------------------------------------------------------
* resource ID and type constant to determine the icon editor
* application
*--------------------------------------------------------------
*/
enum {
kDataBaseID = 128
};
enum {
rFolderType = 'FLDR',
rEditorType = 'EDTR'
};
/*
*--------------------------------------------------------------
* 'STR#' ID and index in the Finder which designates
* the hidden custom icon file name
*--------------------------------------------------------------
*/
enum {
kIconStrID = 11250,
kIconStrIdx = 5
};
/*
*--------------------------------------------------------------
* static function prototypes
*--------------------------------------------------------------
*/
static OSErr FSpGetIconSpec(const FSSpecPtr, FSSpecPtr);
static OSErr GetIconEditor(FSSpecPtr);
static OSErr GetMyLocation(FSSpecPtr);
static OSErr FindAppFromItsSig(const OSType, FSSpecPtr);
static OSErr GetIconFileName(StringPtr);
/*
*--------------------------------------------------------------
* static global which specifiles the icon edotor
*--------------------------------------------------------------
*/
static FSSpec gIconEditor;
/*
*--------------------------------------------------------------
* SetUpMyEditor
*--------------------------------------------------------------
* prepare the icon editor spec
*--------------------------------------------------------------
*/
Boolean SetUpMyEditor(void)
{
OSErr result = GetIconEditor(&gIconEditor);
if (result != noErr) {
NoEditorAlert();
}
return (result == noErr);
}
/*
*--------------------------------------------------------------
* OpenFolderIcon
*--------------------------------------------------------------
* Get a "Icon" file in the given folder spec and pass it to
* ResEdit
*--------------------------------------------------------------
*/
OSErr OpenFolderIcon(FSSpecPtr theHFSSpec)
{
FSSpec iconSpec;
Boolean isFolder, wasAlias;
OSErr result;
result = ResolveAliasFile(theHFSSpec, true, &isFolder, &wasAlias);
if (result == noErr) {
if (isFolder) {
if (IsKeyPressed(kCommandKey)) {
/* just copy icon suite to clipboard */
result = GetIconToScrap(theHFSSpec);
} else {
/* open a icon file using the certain app */
result = FSpGetIconSpec(theHFSSpec, &iconSpec);
if (result == noErr) {
result = PassFileToApp(&gIconEditor, &iconSpec);
}
}
} else {
/* just pass the file */
result = PassFileToApp(&gIconEditor, theHFSSpec);
}
}
return (result);
}
/*
*--------------------------------------------------------------
* FSpGetIconSpec
*--------------------------------------------------------------
* make an Icon File spec in the Folder
*--------------------------------------------------------------
*/
static OSErr FSpGetIconSpec(const FSSpecPtr theFolder, FSSpecPtr theIconSpec)
{
CInfoPBRec aPB;
Str63 tempName;
OSErr result;
/* Protection against File Sharing problem */
if ((theFolder->name == nil) || (theFolder->name[0] == 0)) {
tempName[0] = 0;
aPB.hFileInfo.ioNamePtr = tempName;
aPB.hFileInfo.ioFDirIndex = -1; /* use ioDirID only */
} else {
aPB.hFileInfo.ioNamePtr = theFolder->name;
aPB.hFileInfo.ioFDirIndex = 0; /* use ioNamePtr and ioDirID */
}
aPB.hFileInfo.ioCompletion = nil;
aPB.hFileInfo.ioVRefNum = theFolder->vRefNum;
aPB.hFileInfo.ioDirID = theFolder->parID;
result = PBGetCatInfoSync(&aPB);
if (result == noErr) {
if ((aPB.dirInfo.ioFlAttrib & ioDirMask) == 0) {
NoFolderAlert(theFolder->name);
return (dirNFErr);
}
/* make a hidde icon file spec */
result = GetIconFileName(tempName);
if (result == noErr) {
result = FSMakeFSSpec(
theFolder->vRefNum, aPB.dirInfo.ioDrDirID, tempName, theIconSpec
);
}
if (result == noErr) {
/* set custom icon bit for safety */
aPB.dirInfo.ioDrUsrWds.frFlags |= kHasCustomIcon;
} else {
/* reset custom icon bit for safety */
aPB.dirInfo.ioDrUsrWds.frFlags &= ~kHasCustomIcon;
}
aPB.hFileInfo.ioCompletion = nil;
aPB.hFileInfo.ioVRefNum = theFolder->vRefNum;
aPB.hFileInfo.ioDirID = theFolder->parID;
result = PBSetCatInfoSync(&aPB);
}
if (result != noErr) {
NoIconAlert(theFolder->name);
}
return (result);
}
/*
*--------------------------------------------------------------
* GetIconEditor
*--------------------------------------------------------------
* get an icon editor spec by a couple of search methods
*--------------------------------------------------------------
*/
static OSErr GetIconEditor(FSSpecPtr theAppSpec)
{
Handle aDataHandle = nil;
Str63 aName;
FSSpec mySpec, folderSpec;
OSErr result;
/* get a folder name from a database resource */
aDataHandle = Get1Resource(rFolderType, kDataBaseID);
if (aDataHandle != nil) {
HLock(aDataHandle);
PStrCpy((StringPtr)*aDataHandle, aName);
HUnlock(aDataHandle);
ReleaseResource(aDataHandle);
} else {
result = resNotFound;
}
/* find a folder which contains icon editor application */
if (result == noErr) {
result = GetMyLocation(&mySpec);
}
if (result == noErr) {
result = FSMakeFSSpec(mySpec.vRefNum, mySpec.parID, aName, &folderSpec);
if (result != noErr) {
long folderID;
FSpDirCreate(&folderSpec, smRoman, &folderID);
}
}
/* find an editor application */
if (result == noErr) {
CInfoPBRec aPB;
/* get a folder's info */
aPB.dirInfo.ioNamePtr = folderSpec.name;
aPB.dirInfo.ioVRefNum = folderSpec.vRefNum;
aPB.dirInfo.ioDrDirID = folderSpec.parID;
aPB.dirInfo.ioFDirIndex = 0;
result = PBGetCatInfoSync((CInfoPBPtr)&aPB);
if (result == noErr) {
long aDirID = aPB.dirInfo.ioDrDirID;
short aVRefNum = aPB.dirInfo.ioVRefNum;
short idx = 1;
/* search files in the folder */
do {
aName[0] = 0;
aPB.hFileInfo.ioNamePtr = aName;
aPB.hFileInfo.ioVRefNum = aVRefNum;
aPB.hFileInfo.ioDirID = aDirID;
aPB.hFileInfo.ioFDirIndex = idx++;
result = PBGetCatInfoSync((CInfoPBPtr)&aPB);
if (result == noErr) {
result = FSMakeFSSpec(aVRefNum, aDirID, aName, theAppSpec);
}
if (result == noErr) {
Boolean isFolder, wasAlias;
result = ResolveAliasFile(theAppSpec, true, &isFolder, &wasAlias);
}
if (result == noErr) {
FInfo appsInfo;
result = FSpGetFInfo(theAppSpec, &appsInfo);
if (appsInfo.fdType == 'APPL') {
/* break the loop */
return (result);
}
}
} while (result == noErr);
}
}
if (result != noErr) {
aDataHandle = Get1Resource(rEditorType, kDataBaseID);
if (aDataHandle != nil) {
OSType aCreator = **((OSType **)aDataHandle);
ReleaseResource(aDataHandle);
/* find the application from its signature */
result = FindAppFromItsSig(aCreator, theAppSpec);
}
}
return (result);
}
/*
*--------------------------------------------------------------
* GetMyLocation
*--------------------------------------------------------------
* get FSSpec if myself
*--------------------------------------------------------------
*/
static OSErr GetMyLocation(FSSpecPtr theSpec)
{
ProcessSerialNumber myPSN;
OSErr result;
result = GetCurrentProcess(&myPSN);
if (result == noErr) {
ProcessInfoRec myInfo;
Str63 processName;
myInfo.processName = processName;
myInfo.processAppSpec = theSpec;
result = GetProcessInformation(&myPSN, &myInfo);
}
return (result);
}
/*--------------------------------------------------------------
* FindAppFromItsSig
*--------------------------------------------------------------
* find a process from the application's signature
*--------------------------------------------------------------
*/
static OSErr FindAppFromItsSig(const OSType theSig, FSSpecPtr theSpec)
{
DTPBRec aDTPBRec;
DTPBPtr aDBP;
VolumeParam *aVLP;
HFileParam *aFIP;
Str63 itsName;
short volIndex;
OSErr result;
/* substitute pointers */
aDBP = &aDTPBRec;
aVLP = (VolumeParam *)aDBP;
aFIP = (HFileParam *)aDBP;
/* search the application */
volIndex = 0;
do {
/* get a volume info */
itsName[0] = 0;
aVLP->ioCompletion = nil;
aVLP->ioVolIndex = ++volIndex;
aVLP->ioNamePtr = itsName;
result = PBGetVInfoSync((ParmBlkPtr)aVLP);
/* get a desktop file path */
if (result == noErr) {
result = PBDTGetPath(aDBP);
}
/* get an application info of the volume */
if (result == noErr) {
itsName[0] = 0;
aDBP->ioNamePtr = itsName;
aDBP->ioIndex = 0;
aDBP->ioFileCreator = theSig;
result = PBDTGetAPPLSync(aDBP);
}
/* confirm the result */
if (result == noErr) {
/* once copy answers */
theSpec->vRefNum = aDBP->ioVRefNum;
theSpec->parID = aDBP->ioAPPLParID;
BlockMove(itsName, theSpec->name, *itsName +1);
aFIP->ioFVersNum = 0;
aFIP->ioFDirIndex = 0;
aFIP->ioVRefNum = theSpec->vRefNum;
aFIP->ioDirID = theSpec->parID;
result = PBHGetFInfoSync((HParmBlkPtr)aFIP);
}
if (result == noErr) {
if (aFIP->ioFlFndrInfo.fdType != 'APPL') {
result = afpItemNotFound;
}
}
if (result == noErr) {
return noErr;
}
} while (result != nsvErr); /* until no such volume */
return (result);
}
/*
*--------------------------------------------------------------
* GetIconFileName
*--------------------------------------------------------------
* get a file name of the hidden custom icon file
* From Finder resource
*--------------------------------------------------------------
*/
static OSErr GetIconFileName(StringPtr theName)
{
FSSpec finderSpec;
short finderRef;
OSErr result;
result = FindFolder(kOnSystemDisk, kSystemFolderType,
kDontCreateFolder, &finderSpec.vRefNum, &finderSpec.parID);
if (result == noErr) {
result = FSMakeFSSpec(finderSpec.vRefNum, finderSpec.parID,
"¥pFinder", &finderSpec);
}
if (result == noErr) {
SetResLoad(false);
finderRef = FSpOpenResFile(&finderSpec, fsRdPerm);
result = ResError();
SetResLoad(true);
}
if (result == noErr) {
UseResFile(finderRef);
GetIndString(theName, kIconStrID, kIconStrIdx);
CloseResFile(finderRef);
}
return (result);
}